package com.txunda.zhuohired.aty.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ants.theantsgo.config.Config;
import com.ants.theantsgo.tools.RegexUtils;
import com.ants.theantsgo.util.JSONUtils;
import com.ants.theantsgo.util.PreferencesUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.Base2Aty;
import com.txunda.zhuohired.http.pst.AccountPst;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/10 9:06
 * 功能描述：登录
 * 联系方式：jiahuanchen@126.com
 */

public class LoginAty extends Base2Aty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_blue_center)
    private TextView tv_layout_blue_center;
    /**
     * 注册
     */
    @ViewInject(R.id.tv_layout_blue_right)
    private TextView tv_layout_blue_right;
    /**
     * 手机号
     */
    @ViewInject(R.id.et_login_phone)
    private EditText et_login_phone;
    /**
     * 密码
     */
    @ViewInject(R.id.et_login_pass)
    private EditText et_login_pass;

    private String phone = "";
    private String pass = "";
    private AccountPst accountPst;
    private Bundle bundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_blue_head);
        tv_layout_blue_center.setText("登录");
        tv_layout_blue_right.setVisibility(View.VISIBLE);
        tv_layout_blue_right.setText("注册");
        if (Config.isLogin()) {
            startActivity(SelectIdentityAty.class, null);
            finish();
        }
    }

    @Override
    @OnClick({R.id.tv_layout_blue_right, R.id.tv_login_sure, R.id.tv_login_forget_pass})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_blue_right://注册
                startActivity(RegisterAty.class, null);
                break;
            case R.id.tv_login_sure://登录
                phone = et_login_phone.getText().toString().trim();
                pass = et_login_pass.getText().toString().trim();
                if (!RegexUtils.checkMobile(phone)) {
                    showErrorTip("请检查手机号");
                    return;
                }
                if (TextUtils.isEmpty(pass)) {
                    showErrorTip("请输入密码");
                    return;
                }
                accountPst.login(phone, pass);
                //
                break;
            case R.id.tv_login_forget_pass://忘记密码
                startActivity(ForgetPassAty.class, null);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_login;
    }

    @Override
    protected void initialized() {
        accountPst = new AccountPst(this);
        if (!"".equals(PreferencesUtils.getString(LoginAty.this, "phone"))) {
            et_login_phone.setText(PreferencesUtils.getString(LoginAty.this, "phone"));
        }
    }

    @Override
    protected void requestData() {

    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("login")) {
            //            L.e("=========jsonStr=========", jsonStr);
            PreferencesUtils.putString(LoginAty.this, "phone", phone);
            //            PreferencesUtils.putString(LoginAty.this, "phone", phone);
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            application.setUserInfo(map);
            Config.setLoginState(true);
            bundle = new Bundle();
            bundle.putString("data", map.get("data"));
            startActivity(SelectIdentityAty.class, bundle);
            finish();
            return;
        }
    }
}
